package com.shushan.lockscreen;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shushan.base.Constants;
import com.shushan.home.TabHomeActivity;
import com.shushan.service.DialRTAService;
import com.shushan.service.LockOutRTAService;
import com.shushan.shushanbao.HomeSwitchActivity;
import com.shushan.shushanbao.R;
import com.shushan.util.SystemUtils;

/**
 * @ClassName: SplashFloatWindow
 * @Desciption: 闪屏悬浮窗
 * @author: yichaohua
 * @date: 2018-08-16
 */
public class SplashFloatWindow  extends LinearLayout implements View.OnClickListener {

    private static final String TAG = "SplashFloatWindow";

    private Context mContext;

    private LinearLayout gotoLayout;
//    private TextView countDownText;
    private int countDown = 1;

    private Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    countDownTime();
                    break;
                default:
                    break;
            }

        }
    };

    public SplashFloatWindow(Context context) {
        super(context);
        this.mContext = context;

        LayoutInflater.from(mContext).inflate(R.layout.splash_window, this);

        initView();
    }

    private void initView() {
        gotoLayout = findViewById(R.id.splash_goto_layout);
        gotoLayout.setOnClickListener(this);
//        countDownText = findViewById(R.id.splash_goto_tv);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.splash_goto_layout:
                gotoOver();
                break;
            default:
                break;
        }
    }

    private void gotoOver() {
        SplashWindowManager.removeEyeFloatWindow(mContext);

        boolean isRtaAlive = SystemUtils.isServiceWork(mContext, Constants.FOREGROUND_RTA_SERVICE_CLASS_NAME);
        Log.i(TAG, "isRtaAlive=" + isRtaAlive);
        if(!isRtaAlive){
            SystemUtils.sendForegActivity(mContext, TabHomeActivity.class);
            Intent intent = new Intent(mContext,LockOutRTAService.class);
            mContext.startService(intent);
        }
    }

    private void countDownTime() {
        int mTime = countDown;
        if (mTime > 0) {
//            countDownText.setText(countDown + "");
            handler.sendEmptyMessageDelayed(0, 1000);
            countDown--;
        }else {
            gotoOver();
        }
    }

    public void setStartGone() {
        countDown = 1;
//        countDownText.setText("1");
    }

    public void startCount(){
        handler.sendEmptyMessageDelayed(0,1000);
    }
}
